package com.screen.screendisplay.controller;

import com.screen.screendisplay.common.pojo.ApiResponse;
import com.screen.screendisplay.service.ne.NeProdoperPowerGsService;
import com.screen.screendisplay.service.ne.NeProdoperPowerTotalService;
import com.screen.screendisplay.service.ne.NeProdoperElectricEffService;
import com.screen.screendisplay.service.ne.NeProdoperTimeRateService;
import com.screen.screendisplay.service.ne.NeProdoperEastPriceService;
import com.screen.screendisplay.service.ne.NeProdoperWestPriceService;
import com.screen.screendisplay.service.ne.NeProdoperSalesIncomeService;
import com.screen.screendisplay.service.ne.NeProdoperSalesPurchaseService;
import com.screen.screendisplay.service.ne.NeOverviewItemService;
import com.screen.screendisplay.service.ne.NeOpermonEqpStateService;
import com.screen.screendisplay.service.ne.NeOpermonWindStateService;
import com.screen.screendisplay.service.ne.NeOpermonPvStateService;
import com.screen.screendisplay.service.ne.NeOpermonRtLoadService;
import com.screen.screendisplay.service.ne.NeOpermonRtPvService;
import com.screen.screendisplay.service.ne.NeOpermonRtWindService;
import com.screen.screendisplay.service.ne.NeSafetyenvCoalReduceService;
import com.screen.screendisplay.service.ne.NeSafetyenvRiskTaskService;
import com.screen.screendisplay.service.ne.NeSafetyenvNetRankService;
import com.screen.screendisplay.vo.ne.NePowerGenerationAnnualVO;
import com.screen.screendisplay.vo.ne.NePowerGenerationMonthlyVO;
import com.screen.screendisplay.vo.ne.NePowerGenerationDailyVO;
import com.screen.screendisplay.vo.ne.NePowerGenerationYoyVO;
import com.screen.screendisplay.vo.ne.NeElectricEfficiencyVO;
import com.screen.screendisplay.vo.ne.NeTimeUtilizationRateVO;
import com.screen.screendisplay.vo.ne.NeEastElectricPriceVO;
import com.screen.screendisplay.vo.ne.NeWestElectricPriceVO;
import com.screen.screendisplay.vo.ne.NeSalesIncomeVO;
import com.screen.screendisplay.vo.ne.NeSalesPurchaseVO;
import com.screen.screendisplay.vo.ne.NeOverviewIndicatorsVO;
import com.screen.screendisplay.vo.ne.NeEquipmentOverviewVO;
import com.screen.screendisplay.vo.ne.NeWindEquipmentStatusVO;
import com.screen.screendisplay.vo.ne.NePvEquipmentStatusVO;
import com.screen.screendisplay.vo.ne.NeRealTimeLoadVO;
import com.screen.screendisplay.vo.ne.NePvRealPowerVO;
import com.screen.screendisplay.vo.ne.NeWindRealPowerVO;
import com.screen.screendisplay.vo.ne.NeCoalReductionVO;
import com.screen.screendisplay.vo.ne.NeHighRiskWorkVO;
import com.screen.screendisplay.vo.ne.NePlatformRankingVO;
import com.screen.screendisplay.vo.ne.WindCompanyIntroVO;
import com.screen.screendisplay.vo.ne.WindFieldBasicInfoVO;
import com.screen.screendisplay.vo.ne.WindSafetyEnvVO;
import com.screen.screendisplay.vo.ne.WindPowerGenerationVO;
import com.screen.screendisplay.vo.ne.WindResourceVO;
import com.screen.screendisplay.vo.ne.WindRealTimePowerVO;
import com.screen.screendisplay.vo.ne.WindGridPowerVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 能源消费表 前端控制器
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Tag(name = "新能源产业", description = "新能源产业")
@RestController
@RequestMapping("/ne")
public class NeController {

    @Autowired
    private NeProdoperPowerGsService neProdoperPowerGsService;

    @Autowired
    private NeProdoperPowerTotalService neProdoperPowerTotalService;

    @Autowired
    private NeProdoperElectricEffService neProdoperElectricEffService;

    @Autowired
    private NeProdoperTimeRateService neProdoperTimeRateService;

    @Autowired
    private NeProdoperEastPriceService neProdoperEastPriceService;

    @Autowired
    private NeProdoperWestPriceService neProdoperWestPriceService;

    @Autowired
    private NeProdoperSalesIncomeService neProdoperSalesIncomeService;

    @Autowired
    private NeProdoperSalesPurchaseService neProdoperSalesPurchaseService;

    @Autowired
    private NeOverviewItemService neOverviewItemService;

    @Autowired
    private NeOpermonEqpStateService neOpermonEqpStateService;

    @Autowired
    private NeOpermonWindStateService neOpermonWindStateService;

    @Autowired
    private NeOpermonPvStateService neOpermonPvStateService;

    @Autowired
    private NeOpermonRtLoadService neOpermonRtLoadService;

    @Autowired
    private NeOpermonRtPvService neOpermonRtPvService;

    @Autowired
    private NeOpermonRtWindService neOpermonRtWindService;

    @Autowired
    private NeSafetyenvCoalReduceService neSafetyenvCoalReduceService;
    
    @Autowired
    private NeSafetyenvRiskTaskService neSafetyenvRiskTaskService;
    
    @Autowired
    private NeSafetyenvNetRankService neSafetyenvNetRankService;


    @Operation(summary = "生产经营-发电情况-年发电完成情况", description = "生产经营-发电情况-年发电完成情况")
    @GetMapping(value = "/operation/power-generation/annual")
    public ApiResponse<List<NePowerGenerationAnnualVO>> getAnnualPowerGeneration(@RequestParam(value = "year", required = false) Integer year) {
        List<NePowerGenerationAnnualVO> resultList = neProdoperPowerGsService.getAnnualPowerGeneration(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-发电情况-月发电完成情况", description = "生产经营-发电情况-月发电完成情况")
    @GetMapping(value = "/operation/power-generation/monthly")
    public ApiResponse<List<NePowerGenerationMonthlyVO>> getMonthlyPowerGeneration(@RequestParam(value = "year", required = false) Integer year) {
        List<NePowerGenerationMonthlyVO> resultList = neProdoperPowerGsService.getMonthlyPowerGeneration(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-发电情况-日发电完成情况", description = "生产经营-发电情况-日发电完成情况")
    @GetMapping(value = "/operation/power-generation/daily")
    public ApiResponse<List<NePowerGenerationDailyVO>> getDailyPowerGeneration(@RequestParam(value = "year", required = false) Integer year) {
        List<NePowerGenerationDailyVO> resultList = neProdoperPowerGsService.getDailyPowerGeneration(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-生产情况-发电量月度同比情况", description = "生产经营-生产情况-发电量月度同比情况")
    @GetMapping(value = "/operation/production/yoy-rate")
    public ApiResponse<List<NePowerGenerationYoyVO>> getPowerGenerationYoyRate(@RequestParam(value = "year", required = false) Integer year) {
        List<NePowerGenerationYoyVO> resultList = neProdoperPowerTotalService.getPowerGenerationYoyRate(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-生产情况-电力生产效率", description = "生产经营-生产情况-电力生产效率")
    @GetMapping(value = "/operation/production/efficiency")
    public ApiResponse<List<NeElectricEfficiencyVO>> getElectricProductionEfficiency(@RequestParam(value = "year", required = false) Integer year) {
        List<NeElectricEfficiencyVO> resultList = neProdoperElectricEffService.getElectricProductionEfficiency(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-生产情况-风电/光伏时间利用率", description = "生产经营-生产情况-风电/光伏时间利用率")
    @GetMapping(value = "/operation/production/utilization-rate")
    public ApiResponse<List<NeTimeUtilizationRateVO>> getTimeUtilizationRate(@RequestParam(value = "year", required = false) Integer year) {
        List<NeTimeUtilizationRateVO> resultList = neProdoperTimeRateService.getTimeUtilizationRate(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-动态电价情况-蒙东实时电价", description = "生产经营-动态电价情况-蒙东实时电价")
    @GetMapping(value = "/operation/electric-price/east")
    public ApiResponse<List<NeEastElectricPriceVO>> getEastElectricPrice(@RequestParam(value = "year", required = false) Integer year) {
        List<NeEastElectricPriceVO> resultList = neProdoperEastPriceService.getEastElectricPrice(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-动态电价情况-蒙西实时电价", description = "生产经营-动态电价情况-蒙西实时电价")
    @GetMapping(value = "/operation/electric-price/west")
    public ApiResponse<List<NeWestElectricPriceVO>> getWestElectricPrice(@RequestParam(value = "year", required = false) Integer year) {
        List<NeWestElectricPriceVO> resultList = neProdoperWestPriceService.getWestElectricPrice(year);
        return ApiResponse.succeed(resultList);
    }


    @Operation(summary = "生产经营-销售情况-营收情况", description = "生产经营-销售情况-营收情况")
    @GetMapping(value = "/operation/sales/income")
    public ApiResponse<List<NeSalesIncomeVO>> getSalesIncome(@RequestParam(value = "year", required = false) Integer year) {
        List<NeSalesIncomeVO> resultList = neProdoperSalesIncomeService.getSalesIncome(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-销售情况-购网电量", description = "生产经营-销售情况-购网电量")
    @GetMapping(value = "/operation/sales/purchase")
    public ApiResponse<List<NeSalesPurchaseVO>> getSalesPurchase(@RequestParam(value = "year", required = false) Integer year) {
        List<NeSalesPurchaseVO> resultList = neProdoperSalesPurchaseService.getSalesPurchase(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "产业指标-产业顶部指标", description = "产业指标-产业顶部指标")
    @GetMapping(value = "/overview/main-indicators")
    public ApiResponse<NeOverviewIndicatorsVO> getMainIndicators(@RequestParam(value = "year", required = false) Integer year) {
        NeOverviewIndicatorsVO vo = neOverviewItemService.getMainIndicators(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "运行监测-设备状态监测-设备总体运行情况", description = "运行监测-设备状态监测-设备总体运行情况")
    @GetMapping(value = "/monitor/equipment/overview")
    public ApiResponse<NeEquipmentOverviewVO> getEquipmentOverview(@RequestParam(value = "year", required = false) Integer year) {
        NeEquipmentOverviewVO vo = neOpermonEqpStateService.getEquipmentOverview(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "运行监测-设备状态监测-风电设备状态", description = "运行监测-设备状态监测-风电设备状态")
    @GetMapping(value = "/monitor/equipment/wind-status")
    public ApiResponse<NeWindEquipmentStatusVO> getWindEquipmentStatus(@RequestParam(value = "year", required = false) Integer year) {
        NeWindEquipmentStatusVO vo = neOpermonWindStateService.getWindEquipmentStatus(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "运行监测-设备状态监测-光伏设备状态", description = "运行监测-设备状态监测-光伏设备状态")
    @GetMapping(value = "/monitor/equipment/pv-status")
    public ApiResponse<NePvEquipmentStatusVO> getPvEquipmentStatus(@RequestParam(value = "year", required = false) Integer year) {
        NePvEquipmentStatusVO vo = neOpermonPvStateService.getPvEquipmentStatus(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "运行监测-电厂运行监测-总实时负荷", description = "运行监测-电厂运行监测-总实时负荷")
    @GetMapping(value = "/monitor/plant/real-time-load")
    public ApiResponse<List<NeRealTimeLoadVO>> getRealTimeLoad(@RequestParam(value = "year", required = false) Integer year) {
        List<NeRealTimeLoadVO> resultList = neOpermonRtLoadService.getRealTimeLoad(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "运行监测-电厂运行监测-光伏实时功率", description = "运行监测-电厂运行监测-光伏实时功率")
    @GetMapping(value = "/monitor/plant/pv-real-power")
    public ApiResponse<List<NePvRealPowerVO>> getPvRealPower(@RequestParam(value = "year", required = false) Integer year) {
        List<NePvRealPowerVO> resultList = neOpermonRtPvService.getPvRealPower(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "运行监测-电厂运行监测-风电实时功率", description = "运行监测-电厂运行监测-风电实时功率")
    @GetMapping(value = "/monitor/plant/wind-real-power")
    public ApiResponse<List<NeWindRealPowerVO>> getWindRealPower(@RequestParam(value = "year", required = false) Integer year) {
        List<NeWindRealPowerVO> resultList = neOpermonRtWindService.getWindRealPower(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "安全环保-节煤减排量", description = "安全环保-节煤减排量")
    @GetMapping(value = "/safe/environment/coal-reduction")
    public ApiResponse<List<NeCoalReductionVO>> getCoalReduction(@RequestParam(value = "year", required = false) Integer year) {
        List<NeCoalReductionVO> resultList = neSafetyenvCoalReduceService.getCoalReduction(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "运行监测-高风险作业", description = "运行监测-高风险作业")
    @GetMapping(value = "/safe/environment/high-risk-work")
    public ApiResponse<List<NeHighRiskWorkVO>> getHighRiskWork(@RequestParam(value = "year", required = false) Integer year) {
        List<NeHighRiskWorkVO> resultList = neSafetyenvRiskTaskService.getHighRiskWork(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "安全环保-互联网+平台应用排名", description = "安全环保-互联网+平台应用排名")
    @GetMapping(value = "/safe/environment/platform-ranking")
    public ApiResponse<List<NePlatformRankingVO>> getPlatformRanking(@RequestParam(value = "year", required = false) Integer year) {
        List<NePlatformRankingVO> resultList = neSafetyenvNetRankService.getPlatformRanking(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "新能源公司简介", description = "风电公司简介")
    @GetMapping(value = "/wind/company/intro")
    public ApiResponse<WindCompanyIntroVO> getWindCompanyIntro(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WindCompanyIntroVO vo = new WindCompanyIntroVO();
        
        // 根据unitCode设置公司名称
        String companyName = "察哈尔新能源公司"; // 默认值
        if (unitCode != null) {
            switch (unitCode) {
                case "F_01_01":
                    companyName = "蒙东协和新能源公司";
                    break;
                case "F_01_02":
                    companyName = "察哈尔新能源公司";
                    break;
                case "F_01_03":
                    companyName = "山西新能源公司";
                    break;
                case "F_01_04":
                    companyName = "巴音新能源公司";
                    break;
                case "F_01_05":
                    companyName = "锡盟新能源公司";
                    break;
                case "F_01_06":
                    companyName = "蒙西新能源公司";
                    break;
                case "F_01_07":
                    companyName = "通辽新能源生态建设公司";
                    break;
                case "F_01_08":
                    companyName = "开鲁智慧能源公司（库伦新能源公司）";
                    break;
                default:
                    companyName = "察哈尔新能源公司";
                    break;
            }
        }
        
        vo.setCompanyName(companyName);
        vo.setImageUrl("/images/wind/company.jpg");
        vo.setIntroduction("内蒙古察哈尔新能源有限公司是国家电投集团内蒙古蒙东能源有限公司的全资子公司，成立于2018年7月，位于内蒙古自治区乌兰察布市，公司主要负责乌兰察布风电基地一期600万千瓦范围内的建设、运营和管理工作。项目场址位于乌兰察布市四子王旗内，总投资373亿元，所发电量并送至京津冀地区消纳。公司以打造全国规模最大、回报率最高的风电基地为目标，通过技术创新和管理优化，推动风电产业高质量发展。");
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "风电场基本信息", description = "风电场基本信息")
    @GetMapping(value = "/wind/field/basic-info")
    public ApiResponse<WindFieldBasicInfoVO> getWindFieldBasicInfo(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WindFieldBasicInfoVO vo = new WindFieldBasicInfoVO();
        vo.setWindPowerCapacity(new BigDecimal("7500"));
        vo.setWindFieldCount(100);
        vo.setDailyUtilizationHours(107);
        vo.setWindEquipmentCount(145);
        vo.setWindPowerForecast(new BigDecimal("1000"));
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "风电安全环保", description = "风电安全环保")
    @GetMapping(value = "/wind/safety-env")
    public ApiResponse<WindSafetyEnvVO> getWindSafetyEnv(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WindSafetyEnvVO vo = new WindSafetyEnvVO();
        
        // 节约标煤趋势数据
        List<WindSafetyEnvVO.CoalSavingItem> coalSavingTrend = new ArrayList<>();
        String[] dates = {"19日", "20日", "21日", "22日", "23日", "24日", "25日"};
        int[] coalSavings = {80, 75, 85, 78, 82, 88, 90};
        for (int i = 0; i < dates.length; i++) {
            WindSafetyEnvVO.CoalSavingItem item = new WindSafetyEnvVO.CoalSavingItem();
            item.setDate(dates[i]);
            item.setCoalSaving(new BigDecimal(coalSavings[i]));
            coalSavingTrend.add(item);
        }
        vo.setCoalSavingTrend(coalSavingTrend);
        
        // 二氧化碳减排趋势数据
        List<WindSafetyEnvVO.Co2ReductionItem> co2ReductionTrend = new ArrayList<>();
        for (int i = 0; i < dates.length; i++) {
            WindSafetyEnvVO.Co2ReductionItem item = new WindSafetyEnvVO.Co2ReductionItem();
            item.setDate(dates[i]);
            item.setCo2Reduction(new BigDecimal(coalSavings[i] * 2)); // 约为标煤的2倍
            item.setYoyRate(i == 6 ? -32 : 0); // 最后一天显示同比-32%
            co2ReductionTrend.add(item);
        }
        vo.setCo2ReductionTrend(co2ReductionTrend);
        
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "风电发电情况", description = "风电发电情况")
    @GetMapping(value = "/wind/power-generation")
    public ApiResponse<WindPowerGenerationVO> getWindPowerGeneration(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WindPowerGenerationVO vo = new WindPowerGenerationVO();
        
        List<WindPowerGenerationVO.PowerGenerationItem> trendData = new ArrayList<>();
        String[] times = {"00:00", "04:00", "08:00", "12:00", "16:00", "20:00", "24:00"};
        for (String time : times) {
            WindPowerGenerationVO.PowerGenerationItem item = new WindPowerGenerationVO.PowerGenerationItem();
            item.setTime(time);
            // 生成波动的发电量数据
            double baseValue = 50.0;
            double variation = Math.random() * 30;
            item.setPowerGeneration(new BigDecimal(String.format("%.2f", baseValue + variation)));
            trendData.add(item);
        }
        vo.setTrendData(trendData);
        
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "风电资源", description = "风电资源")
    @GetMapping(value = "/wind/resource")
    public ApiResponse<WindResourceVO> getWindResource(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WindResourceVO vo = new WindResourceVO();
        vo.setAverageWindSpeed(new BigDecimal("8"));
        vo.setCurrentWindSpeed(new BigDecimal("8"));
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "风电实时功率", description = "风电实时功率")
    @GetMapping(value = "/wind/real-time-power")
    public ApiResponse<WindRealTimePowerVO> getWindRealTimePower(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WindRealTimePowerVO vo = new WindRealTimePowerVO();
        
        List<WindRealTimePowerVO.RealTimePowerItem> trendData = new ArrayList<>();
        String[] times = {"00:00", "04:00", "08:00", "12:00", "16:00", "20:00", "24:00"};
        for (String time : times) {
            WindRealTimePowerVO.RealTimePowerItem item = new WindRealTimePowerVO.RealTimePowerItem();
            item.setTime(time);
            // 生成波动的实时功率数据
            double baseValue = 60.0;
            double variation = Math.random() * 20;
            item.setRealTimePower(new BigDecimal(String.format("%.2f", baseValue + variation)));
            trendData.add(item);
        }
        vo.setTrendData(trendData);
        
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "风电上网及购网电量", description = "风电上网及购网电量")
    @GetMapping(value = "/wind/grid-power")
    public ApiResponse<WindGridPowerVO> getWindGridPower(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WindGridPowerVO vo = new WindGridPowerVO();
        vo.setPurchasePower(new BigDecimal("287"));
        vo.setGridPower(new BigDecimal("287"));
        return ApiResponse.succeed(vo);
    }

}
